#include "com_voe_VoEDtmf.h"
#include "jni_api.h"
#include "OutputParam.h"
#include "voe_dtmf.h"
#include "VoETelephoneEventObserverImpl.h"

static VoETelephoneEventObserverImpl _VoETelephoneEventObserver;

/*
 * Class:     com_voe_VoEDtmf
 * Method:    GetInterface
 * Signature: (J)J
 */
JNIEXPORT jlong JNICALL Java_com_voe_VoEDtmf_GetInterface
	(JNIEnv* env, jclass cls, jlong p_voiceEngine)
{
	return (jlong)webrtc::VoEDtmf::GetInterface((webrtc::VoiceEngine*)p_voiceEngine);
}

/*
 * Class:     com_voe_VoEDtmf
 * Method:    Release
 * Signature: (J)I
 */
JNIEXPORT jint JNICALL Java_com_voe_VoEDtmf_Release
	(JNIEnv* env, jclass cls, jlong p)
{
	webrtc::VoEDtmf* pVoEDtmf = (webrtc::VoEDtmf*)p;
	return pVoEDtmf->Release();
}

/*
 * Class:     com_voe_VoEDtmf
 * Method:    SendTelephoneEvent
 * Signature: (JIIZII)I
 */
JNIEXPORT jint JNICALL Java_com_voe_VoEDtmf_SendTelephoneEvent
	(JNIEnv* env, jclass cls, jlong p, jint channel, jint eventCode, jboolean outOfBand, jint lengthMs, jint attenuationDb)
{
	webrtc::VoEDtmf* pVoEDtmf = (webrtc::VoEDtmf*)p;
	return pVoEDtmf->SendTelephoneEvent(channel, eventCode, outOfBand!=0, lengthMs, attenuationDb);
}

/*
 * Class:     com_voe_VoEDtmf
 * Method:    SetSendTelephoneEventPayloadType
 * Signature: (JIB)I
 */
JNIEXPORT jint JNICALL Java_com_voe_VoEDtmf_SetSendTelephoneEventPayloadType
  (JNIEnv* env, jclass cls, jlong p, jint channel, jbyte type)
{
	webrtc::VoEDtmf* pVoEDtmf = (webrtc::VoEDtmf*)p;
	return pVoEDtmf->SetSendTelephoneEventPayloadType(channel, type);
}

/*
 * Class:     com_voe_VoEDtmf
 * Method:    GetSendTelephoneEventPayloadType
 * Signature: (JILcom/voe/common/OutputParam;)I
 */
JNIEXPORT jint JNICALL Java_com_voe_VoEDtmf_GetSendTelephoneEventPayloadType
	(JNIEnv* env, jclass cls, jlong p, jint channel, jobject obj)
{
	webrtc::VoEDtmf* pVoEDtmf = (webrtc::VoEDtmf*)p;
	unsigned char type;
	jint result = pVoEDtmf->GetSendTelephoneEventPayloadType(channel, type);
	if(result == 0)
	{
		JniOutputBuilder job(env);
		OutputParam param(env, obj);
		param.setArray(job.add((int)type).getOutput());
	}
	return result;
}

/*
 * Class:     com_voe_VoEDtmf
 * Method:    SetDtmfPlayoutStatus
 * Signature: (JIZ)I
 */
JNIEXPORT jint JNICALL Java_com_voe_VoEDtmf_SetDtmfPlayoutStatus
	(JNIEnv* env, jclass cls, jlong p, jint channel, jboolean enable)
{
	webrtc::VoEDtmf* pVoEDtmf = (webrtc::VoEDtmf*)p;
	return pVoEDtmf->SetDtmfPlayoutStatus(channel, enable!=0);
}

/*
 * Class:     com_voe_VoEDtmf
 * Method:    GetDtmfPlayoutStatus
 * Signature: (JILcom/voe/common/OutputParam;)I
 */
JNIEXPORT jint JNICALL Java_com_voe_VoEDtmf_GetDtmfPlayoutStatus
  (JNIEnv* env, jclass cls, jlong p, jint channel, jobject obj)
{
	webrtc::VoEDtmf* pVoEDtmf = (webrtc::VoEDtmf*)p;
	bool enabled;
	jint result = pVoEDtmf->GetDtmfPlayoutStatus(channel, enabled);
	if(result == 0)
	{
		JniOutputBuilder job(env);
		OutputParam param(env, obj);
		param.setArray(job.add(enabled).getOutput());
	}
	return result;
}

/*
 * Class:     com_voe_VoEDtmf
 * Method:    SetDtmfFeedbackStatus
 * Signature: (JZZ)I
 */
JNIEXPORT jint JNICALL Java_com_voe_VoEDtmf_SetDtmfFeedbackStatus
	(JNIEnv* env, jclass cls, jlong p, jboolean enable, jboolean directFeedback)
{
	webrtc::VoEDtmf* pVoEDtmf = (webrtc::VoEDtmf*)p;
	return pVoEDtmf->SetDtmfFeedbackStatus(enable!=0, directFeedback!=0);
}

/*
 * Class:     com_voe_VoEDtmf
 * Method:    GetDtmfFeedbackStatus
 * Signature: (JLcom/voe/common/OutputParam;)I
 */
JNIEXPORT jint JNICALL Java_com_voe_VoEDtmf_GetDtmfFeedbackStatus
  (JNIEnv* env, jclass cls, jlong p, jobject obj)
{
	webrtc::VoEDtmf* pVoEDtmf = (webrtc::VoEDtmf*)p;
	bool enabled, directFeedback;
	jint result = pVoEDtmf->GetDtmfFeedbackStatus(enabled, directFeedback);
	if(result == 0)
	{
		JniOutputBuilder job(env);
		OutputParam param(env, obj);
		param.setArray(job.add(enabled).add(directFeedback).getOutput());
	}
	return result;
}

/*
 * Class:     com_voe_VoEDtmf
 * Method:    PlayDtmfTone
 * Signature: (JIII)I
 */
JNIEXPORT jint JNICALL Java_com_voe_VoEDtmf_PlayDtmfTone
	(JNIEnv* env, jclass cls, jlong p, jint eventCode, jint lengthMs, jint attenuationDb)
{
	webrtc::VoEDtmf* pVoEDtmf = (webrtc::VoEDtmf*)p;
	return pVoEDtmf->PlayDtmfTone(eventCode, lengthMs, attenuationDb);
}

/*
 * Class:     com_voe_VoEDtmf
 * Method:    StartPlayingDtmfTone
 * Signature: (JII)I
 */
JNIEXPORT jint JNICALL Java_com_voe_VoEDtmf_StartPlayingDtmfTone
	(JNIEnv* env, jclass cls, jlong p, jint eventCode, jint attenuationDb)
{
	webrtc::VoEDtmf* pVoEDtmf = (webrtc::VoEDtmf*)p;
	return pVoEDtmf->StartPlayingDtmfTone(eventCode, attenuationDb);
}

/*
 * Class:     com_voe_VoEDtmf
 * Method:    StopPlayingDtmfTone
 * Signature: (J)I
 */
JNIEXPORT jint JNICALL Java_com_voe_VoEDtmf_StopPlayingDtmfTone
	(JNIEnv* env, jclass cls, jlong p)
{
	webrtc::VoEDtmf* pVoEDtmf = (webrtc::VoEDtmf*)p;
	return pVoEDtmf->StopPlayingDtmfTone();
}

/*
 * Class:     com_voe_VoEDtmf
 * Method:    RegisterTelephoneEventDetection
 * Signature: (JIILcom/voe/callback/VoETelephoneEventObserver;)I
 */
JNIEXPORT jint JNICALL Java_com_voe_VoEDtmf_RegisterTelephoneEventDetection
  (JNIEnv* env, jclass cls, jlong p, jint channel, jint detectionMethod, jobject obj)
{
	webrtc::VoEDtmf* pVoEDtmf = (webrtc::VoEDtmf*)p;
	if(_VoETelephoneEventObserver.set(env, obj))
	{
		return pVoEDtmf->RegisterTelephoneEventDetection(channel, (webrtc::TelephoneEventDetectionMethods)detectionMethod,
			_VoETelephoneEventObserver);
	}
	return JCB_Err_Set;
}

/*
 * Class:     com_voe_VoEDtmf
 * Method:    DeRegisterTelephoneEventDetection
 * Signature: (JI)I
 */
JNIEXPORT jint JNICALL Java_com_voe_VoEDtmf_DeRegisterTelephoneEventDetection
	(JNIEnv* env, jclass cls, jlong p, jint channel)
{
	webrtc::VoEDtmf* pVoEDtmf = (webrtc::VoEDtmf*)p;
	jint result = pVoEDtmf->DeRegisterTelephoneEventDetection(channel);
	if(result == 0)
		_VoETelephoneEventObserver.unset(env);
	return result;
}

/*
 * Class:     com_voe_VoEDtmf
 * Method:    GetTelephoneEventDetectionStatus
 * Signature: (JILcom/voe/common/OutputParam;)I
 */
JNIEXPORT jint JNICALL Java_com_voe_VoEDtmf_GetTelephoneEventDetectionStatus
  (JNIEnv* env, jclass cls, jlong p, jint channel, jobject obj)
{
	webrtc::VoEDtmf* pVoEDtmf = (webrtc::VoEDtmf*)p;
	bool enabled;
	webrtc::TelephoneEventDetectionMethods detectionMethod;
	jint result = pVoEDtmf->GetTelephoneEventDetectionStatus(channel, enabled, detectionMethod);
	if(result == 0)
	{
		JniOutputBuilder job(env);
		OutputParam param(env, obj);
		param.setArray(job.add(enabled).add((int)detectionMethod).getOutput());
	}
	return result;
}
